package com.gingko.duominuo.ui.widget

import android.annotation.SuppressLint
import android.os.CountDownTimer
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.gingko.duominuo.App
import com.gingko.duominuo.R
import com.gingko.duominuo.utils.isMobile

class MCountDownTimer(
    private val mTextView: TextView,
    millisInFuture: Long,
    countDownInterval: Long
) : CountDownTimer(millisInFuture, countDownInterval) {
    @SuppressLint("SetTextI18n")
    override fun onTick(millisUntilFinished: Long) {
        mTextView.setTextColor(ContextCompat.getColor(App.instance, R.color.textHint))
        mTextView.text = "${millisUntilFinished / 1000L}s" // 设置倒计时时间
        mTextView.isClickable = false
    }

    override fun onFinish() {
        mTextView.text = "获取验证码"
        setClickable(true)
    }

    private fun setClickable(clickable: Boolean) {
        if (clickable) {
            mTextView.isClickable = true // 重新获得点击
            mTextView.setTextColor(ContextCompat.getColor(App.instance, R.color.textLink))
        } else {
            mTextView.isClickable = false // 设置不可点击
            mTextView.setTextColor(ContextCompat.getColor(App.instance, R.color.textHint))
        }
    }

    init {
        setClickable(false)
    }
}